﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MPI;

namespace MPI.NET_Template
{
    class Program
    {
        static void Main(string[] args)
        {
            //MPI.Environment implicitly calls MPI_Init on creation 
            //and MPI_Finalize on destruction
            using (MPI.Environment env = new MPI.Environment(ref args))
            {
                Intracommunicator world = Communicator.world;
                if (world.Size > 1)
                {
                    if (world.Rank == 0)
                    {
                        //world.Size is determined by the number of
                        //processes start with the MPI job launcher
                        //(i.e. mpiexec -n 3 myProgram.exe)
                        for (int target = 1; target < world.Size; ++target)
                        {
                            world.Send<string>("Hello World!", target, 0);
                            Console.WriteLine("Root sent message to rank {0}", target);
                        }
                    }
                    else
                    {
                        string message = world.Receive<string>(0, 0);
                        Console.WriteLine("Rank {0} received message: {1} from root", world.Rank, message);
                    }
                }
                else
                {
                    throw new ApplicationException("This program must be ran with 2 or more processes");
                }
            }
        }
    }
}
